using Microsoft.Expression.Drawing.Core;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace Microsoft.Expression.Controls
{
	internal class FrameworkElementStrategy : GeometryStrategy
	{
		private FrameworkElement sourceElement;
		private Size size;
		private static readonly DependencyProperty VisibilityListenerProperty = DependencyProperty.Register("Visibility", typeof(Visibility), typeof(FrameworkElementStrategy), new PropertyMetadata(new PropertyChangedCallback(GeometryStrategy.LayoutPropertyChanged)));
		private static readonly DependencyProperty WidthListenerProperty = DependencyProperty.Register("Width", typeof(double), typeof(FrameworkElementStrategy), new PropertyMetadata(new PropertyChangedCallback(FrameworkElementStrategy.LayoutPropertyChangedInCavas)));
		private static readonly DependencyProperty HeightListenerProperty = DependencyProperty.Register("Height", typeof(double), typeof(FrameworkElementStrategy), new PropertyMetadata(new PropertyChangedCallback(FrameworkElementStrategy.LayoutPropertyChangedInCavas)));
		private Visibility VisibilityListener
		{
			get
			{
				return (Visibility)base.GetValue(FrameworkElementStrategy.VisibilityListenerProperty);
			}
			set
			{
				base.SetValue(FrameworkElementStrategy.VisibilityListenerProperty, value);
			}
		}
		private double WidthListener
		{
			get
			{
				return (double)base.GetValue(FrameworkElementStrategy.WidthListenerProperty);
			}
			set
			{
				base.SetValue(FrameworkElementStrategy.WidthListenerProperty, value);
			}
		}
		private double HeightListener
		{
			get
			{
				return (double)base.GetValue(FrameworkElementStrategy.HeightListenerProperty);
			}
			set
			{
				base.SetValue(FrameworkElementStrategy.HeightListenerProperty, value);
			}
		}
		protected Size Size
		{
			get
			{
				if (!this.size.HasValidArea())
				{
					this.size = new Size(this.sourceElement.get_ActualWidth(), this.sourceElement.get_ActualHeight());
				}
				return this.size;
			}
		}
		public FrameworkElementStrategy(LayoutPath layoutPath) : base(layoutPath)
		{
			this.sourceElement = layoutPath.SourceElement;
			this.sourceElement.add_SizeChanged(new SizeChangedEventHandler(this.SourceElement_SizeChanged));
			base.SetListenerBinding(FrameworkElementStrategy.VisibilityListenerProperty, "Visibility");
			if (this.sourceElement.get_Parent() is Canvas)
			{
				base.SetListenerBinding(FrameworkElementStrategy.WidthListenerProperty, "Width");
				base.SetListenerBinding(FrameworkElementStrategy.HeightListenerProperty, "Height");
			}
		}
		public override bool HasGeometryChanged()
		{
			return false;
		}
		protected override PathGeometry UpdateGeometry()
		{
			RectangleGeometry rectangleGeometry = new RectangleGeometry();
			rectangleGeometry.set_Rect(new Rect(default(Point), this.Size));
			return rectangleGeometry.AsPathGeometry();
		}
		public override void Unhook()
		{
			this.sourceElement.remove_SizeChanged(new SizeChangedEventHandler(this.SourceElement_SizeChanged));
			base.Unhook();
		}
		public override IList<GeneralTransform> ComputeTransforms()
		{
			return null;
		}
		private void SourceElement_SizeChanged(object sender, SizeChangedEventArgs e)
		{
			this.size = e.get_NewSize();
			base.LayoutPath.IsLayoutDirty = true;
		}
		protected static void LayoutPropertyChangedInCavas(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			FrameworkElementStrategy frameworkElementStrategy = d as FrameworkElementStrategy;
			if (frameworkElementStrategy != null && frameworkElementStrategy.LayoutPath != null && frameworkElementStrategy.LayoutPath.IsAttached)
			{
				if (e.get_Property() == FrameworkElementStrategy.WidthListenerProperty)
				{
					frameworkElementStrategy.size = new Size((double)e.get_NewValue(), frameworkElementStrategy.size.get_Height());
				}
				else
				{
					if (e.get_Property() == FrameworkElementStrategy.HeightListenerProperty)
					{
						frameworkElementStrategy.size = new Size(frameworkElementStrategy.size.get_Width(), (double)e.get_NewValue());
					}
				}
				frameworkElementStrategy.LayoutPath.IsLayoutDirty = true;
				return;
			}
		}
	}
}
